package com.hdoit.ares.logical.acc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.acc.dao.ICustomerDemoDao;
import com.hdoit.ares.logical.acc.mapper.CustomerDemoMapper;
import com.hdoit.ares.logical.acc.model.CustomerDemo;
import com.hdoit.ares.logical.viewmodel.acc.CustomerDemoSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年11月29日 12:02:13
 * @comment
 */
@Component
public class CustomerDemoDaoImpl extends SuperDao<CustomerDemo> implements ICustomerDemoDao{
    private CustomerDemoMapper customerDemoMapper;
    
	public CustomerDemoMapper getCustomerDemoMapper() {
		return customerDemoMapper;
	}
	@Resource
	public void setCustomerDemoMapper(CustomerDemoMapper customerDemoMapper) {
		this.customerDemoMapper = customerDemoMapper;
	}

	@Override
	public List<CustomerDemo> queryList(CustomerDemoSearch search, PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select(" * ")
		                .from(CustomerDemo.class)
		                .andWhere("deleteMark = #{deleteMark}")
		                .setParam("deleteMark",YesOrNo.no)
		                .addOrder("registDate", OrderEnum.asc);
		 
		if(pagerModel == null){
            return queryList(finderExpression, search); 
          }
        return queryList(finderExpression, search, pagerModel);
	}

	@Override
	public List<CustomerDemo> queryList(CustomerDemoSearch search) {
		return queryList(search,null);
	}

	@Override
	public int queryCount(CustomerDemoSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT id) as count")
		                .from(CustomerDemo.class)
		                .andWhere("deleteMark=#{deleteMark}")
		                .setParam("deleteMark", YesOrNo.no);

		return queryCount(finderExpression, search);
	}

	@Override
	public List<CustomerDemo> queryListByBuyer(String loginId) {
		return customerDemoMapper.queryListByBuyer(loginId);
	}
	@Override
	public int queryMaxUserIdentity() {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("max(userIdentity) as m")
						.from(CustomerDemo.class);
		String scalar=queryScalar(finderExpression);
		if(StringUtils.isEmpty(scalar)){
			return 2000;
		}
		return CommonUtils.getNumber(scalar);
	}

}
